package com.bwie.auth.mq.consumer;

import com.bwie.auth.pojo.bo.AsyncSendCodeBo;
import com.bwie.auth.utils.SmsUtils;
import com.bwie.common.constants.Constants;
import com.bwie.common.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SendCodeConsumer {
    @RabbitListener(queuesToDeclare = @Queue("2209a"))
    public void sendCodeByPhone(AsyncSendCodeBo asyncSendCodeBo){
        String phone = asyncSendCodeBo.getPhone();
        try {
            SmsUtils.sendSms(phone, asyncSendCodeBo.getCode());
        } catch (Exception e) {
            log.error("短信发送失败，手机号：{}", phone, e);
            // 可触发告警或重试机制
            throw new BusinessException(Constants.SEND_ERROR);
        }
    }
}
